<?php

/**
 * @file
 *
 * @package    Europa
 * @subpackage View
 */

/**
 * @class
 *
 * @name Europa_View_Json
 * @desc Renders the current view content as JSON
 */
class Europa_View_Xml extends Europa_View
{
	private static function array_to_xml($array, $level=1) {
		$xml = '';
		if ($level==1) {
			$xml .= '<?xml version="1.0" encoding="ISO-8859-1"?>'.
                "\n<array>\n";
		}
		foreach ($array as $key=>$value) {
			$key = strtolower($key);
			if (is_array($value)) {
				$multi_tags = false;
				foreach($value as $key2=>$value2) {
					if (is_array($value2)) {
						$xml .= str_repeat("\t",$level)."<$key>\n";
						$xml .= array_to_xml($value2, $level+1);
						$xml .= str_repeat("\t",$level)."</$key>\n";
						$multi_tags = true;
					} else {
						if (trim($value2)!='') {
							if (htmlspecialchars($value2)!=$value2) {
								$xml .= str_repeat("\t",$level).
                                    "<$key><![CDATA[$value2]]>".
                                    "</$key>\n";
							} else {
								$xml .= str_repeat("\t",$level).
                                    "<$key>$value2</$key>\n";
							}
						}
						$multi_tags = true;
					}
				}
				if (!$multi_tags and count($value)>0) {
					$xml .= str_repeat("\t",$level)."<$key>\n";
					$xml .= array_to_xml($value, $level+1);
					$xml .= str_repeat("\t",$level)."</$key>\n";
				}
			} else {
				if (trim($value)!='') {
					if (htmlspecialchars($value)!=$value) {
						$xml .= str_repeat("\t",$level)."<$key>".
                            "<![CDATA[$value]]></$key>\n";
					} else {
						$xml .= str_repeat("\t",$level).
                            "<$key>$value</$key>\n";
					}
				}
			}
		}
		if ($level==1) {
			$xml .= "</array>\n";
		}
		return $xml;
	}


	/**
	 * @method
	 * @magic
	 *
	 * @name __toString
	 * @desc Returns the JSON content of the rendered view.
	 *
	 * @return String
	 */
	public function __toString()
	{
		return self::array_to_xml($this->_content);
	}

	/**
	 * @method
	 * @public
	 *
	 * @name render
	 * @desc Sets the script to be rendered.
	 *
	 * @param String $script - The path to the script to be rendered relative to the view path, excluding the extension.
	 *
	 * @return Object Europa_View
	 */
	public function render($script)
	{
		return $this;
	}
}
